﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Graffiti.Core;

namespace GraffitiExtras.Extensions.Sharing
{
	[Chalk("sharing")]
	public class Sharing
	{
		private class ShareItTarget
		{
			public string Name;
			public string UrlFormat;
			public string ImageName;
		}

		private static readonly List<ShareItTarget> _targets;

		static Sharing()
		{
			_targets = new List<ShareItTarget>
			{
				//new ShareItTarget
				//{
				//    Name = "DZone It!",
				//    UrlFormat = "http://www.dzone.com/links/add.html?url={0}&amp;title={1}",
				//    ImageName = "dzone"
				//},
				new ShareItTarget
				{
					Name = "Digg It!",
					UrlFormat = "http://digg.com/submit/?url={0}&title={1}",
					ImageName = "digg"
				},		
				new ShareItTarget
				{
					Name = "StumbleUpon",
					UrlFormat = "http://www.stumbleupon.com/submit?url={0}&title={1}",
					ImageName = "stumbleupon"
				},
				new ShareItTarget
				{
					Name = "Reddit",
					UrlFormat = "http://reddit.com/submit?url={0}&title={1}",
					ImageName = "reddit"
				},
				new ShareItTarget
				{
					Name = "Del.icio.us",
					UrlFormat = "http://del.icio.us/post?url={0}&title={1}",
					ImageName = "delicious"
				},
				new ShareItTarget
				{
					Name = "NewsVine",
					UrlFormat = "http://www.newsvine.com/_wine/save?popoff=0&u={0}",
					ImageName = "newsvine"
				},
				new ShareItTarget
				{
					Name = "Furl",
					UrlFormat = "http://www.furl.net/store?u={0}&ti={1}",
					ImageName = "furl"
				},
				new ShareItTarget
				{
					Name = "BlinkList",
					UrlFormat = "http://www.blinklist.com/?Action=Blink/addblink.php&Url={0}&Title={1}",
					ImageName = "blinklist"
				},
				new ShareItTarget
				{
					Name = "Ma.gnolia",
					UrlFormat = "http://ma.gnolia.com/bookmarklet/add?url={0}&title={1}",
					ImageName = "magnolia"
				},
				new ShareItTarget
				{
					Name = "Technorati",
					UrlFormat = "http://technorati.com/ping?url={0}",
					ImageName = "technorati"
				},
			};
		}

		public string Write(Post post, string imagefolder)
		{
			return Write(post, imagefolder, "&nbsp;");
		}

		public string Write(Post post, string imagefolder, string separator)
		{
			if (post == null)
				throw new ArgumentNullException("post", "post must not be null");
			if (String.IsNullOrEmpty(imagefolder))
				throw new ArgumentNullException("imagefolder", "imagefolder must not be empty");

			string url = HttpUtility.UrlEncode(new Macros().FullUrl(post.Url));
			string title = HttpUtility.UrlEncode(post.Title);

			if (!imagefolder.EndsWith("/"))
				imagefolder += "/";

			List<string> links = new List<string>();
			foreach (ShareItTarget shareItTarget in _targets)
			{
				StringBuilder sb = new StringBuilder();
				sb.Append("<a href=\"");
				sb.AppendFormat(shareItTarget.UrlFormat, url, title);
				sb.Append("\" rel=\"nofollow\">");
				sb.AppendFormat("<img src=\"{0}/{1}.png\" style=\"border: 0;\" title=\"{2}\" alt=\"{2}\" />",
								imagefolder, shareItTarget.ImageName, shareItTarget.Name);
				sb.AppendLine("</a>");
				links.Add(sb.ToString());
			}

			return String.Join(separator, links.ToArray());
		}
	}
}
